package com.liangge.essaysite.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;

import com.liangge.essaysite.bean.Article;
import com.liangge.essaysite.dao.ArticleDao;
import com.liangge.essaysite.service.ArticleService;
import com.liangge.essaysite.util.AgentKeys;
import com.liangge.essaysite.util.Pagination;

public class ArticleServiceImpl implements ArticleService {
	private ArticleDao articleDao;
	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}
	@Override
	public int selectArticleCountByKeys(Map params) {
		return articleDao.selectArticleCountByKeys(params);
	}
	@Override
	public int insertArticle(Article article) {
		//判断是否是需要设置article_id
		if(article.getArticleSource() == AgentKeys.ARTICLE_SOURCE_POSTED){
			int articleId = articleDao.selectIndexPostArticleId();
			article.setArticleId(articleId);
		}
		return articleDao.insertArticle(article);
	}
	@Override
	public Pagination<Article> selectNewArticleList(int pageNo){
		Pagination<Article> page = new Pagination<Article>(pageNo);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("start", page.getDataStart());
		params.put("offset", page.getPageSize());
		List<Article> resultList = articleDao.selectNewArticleList(params);
		int resultCount = articleDao.selectArticleCountByStatus(AgentKeys.ARTICLE_STATUS_ONLINE);
		page.setResultList(resultList);
		page.setTotalCount(resultCount);
		return page;
	}
	@Override
	public Pagination<Article> selectReadArticleList(int pageNo,int limitDays) {
		Pagination<Article> page = new Pagination<Article>(pageNo);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("start", page.getDataStart());
		params.put("offset", page.getPageSize());
		Date preDate = DateUtils.addDays(new Date(), -limitDays);
		params.put("createTime", preDate);
		List<Article> resultList = articleDao.selectReadArticleList(params);
		
		params.clear();
		params.put("status", AgentKeys.ARTICLE_STATUS_ONLINE);
		params.put("createTime", preDate);
		int resultCount = articleDao.selectArticleCountByStatusAndCreateTime(params);
		page.setResultList(resultList);
		page.setTotalCount(resultCount);
		return page;
	}
	@Override
	public Pagination<Article> selectGoodArticleList(int pageNo, int limitDays) {
		Pagination<Article> page = new Pagination<Article>(pageNo);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("start", page.getDataStart());
		params.put("offset", page.getPageSize());
		Date preDate = DateUtils.addDays(new Date(), -limitDays);
		params.put("createTime", preDate);
		List<Article> resultList = articleDao.selectGoodArticleList(params);
		
		params.clear();
		params.put("status", AgentKeys.ARTICLE_STATUS_ONLINE);
		params.put("createTime", preDate);
		int resultCount = articleDao.selectArticleCountByStatusAndCreateTime(params);
		page.setResultList(resultList);
		page.setTotalCount(resultCount);
		return page;
	}
	@Override
	public Pagination<Article> selectCommentArticleList(int pageNo, int limitDays) {
		Pagination<Article> page = new Pagination<Article>(pageNo);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("start", page.getDataStart());
		params.put("offset", page.getPageSize());
		Date preDate = DateUtils.addDays(new Date(), -limitDays);
		params.put("createTime", preDate);
		List<Article> resultList = articleDao.selectCommentArticleList(params);
		
		params.clear();
		params.put("status", AgentKeys.ARTICLE_STATUS_ONLINE);
		params.put("createTime", preDate);
		int resultCount = articleDao.selectArticleCountByStatusAndCreateTime(params);
		page.setResultList(resultList);
		page.setTotalCount(resultCount);
		return page;
	}
	@Override
	public Pagination<Article> selectCategoryArticleList(int pageNo,int categoryId){
		Pagination<Article> page = new Pagination<Article>(pageNo);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("categoryId", categoryId);
		params.put("start", page.getDataStart());
		params.put("offset", page.getPageSize());
		List<Article> resultList = articleDao.selectCategoryArticleList(params);
		
		params.clear();
		params.put("status", AgentKeys.ARTICLE_STATUS_ONLINE);
		params.put("categoryId", categoryId);
		int resultCount = articleDao.selectArticleCountByStatusAndCategoryId(params);
		page.setResultList(resultList);
		page.setTotalCount(resultCount);
		return page;
	}
}
